package com.ljw.community.controller;

import com.ljw.community.entity.AjaxRespInfo;
import com.ljw.community.entity.Comment;
import com.ljw.community.service.CommentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/commentController")
public class CommentController {

    @Resource
    private CommentService commentService;

    @RequestMapping("/loadComments")
    @ResponseBody
    AjaxRespInfo<List<Comment>> loadComments(String topicId) {
        AjaxRespInfo<List<Comment>> ajaxRespInfo = new AjaxRespInfo<>();
        List<Comment> commentsList = commentService.loadComments(topicId);
        if (commentsList != null && !commentsList.isEmpty()) {
            ajaxRespInfo.setCode(0);
            ajaxRespInfo.setData(commentsList);
        } else {
            ajaxRespInfo.setCode(1);
        }
        return ajaxRespInfo;
    }


    /**
     * 发表留言
     */
    @RequestMapping("/comment")
    @ResponseBody
    AjaxRespInfo<List<Comment>> comment(String topicId, String loginName, String content) {
        AjaxRespInfo<List<Comment>> ajaxRespInfo = new AjaxRespInfo<>();
        int rowNum = commentService.comment(topicId,loginName,content);
        if (rowNum == 1) {
            ajaxRespInfo.setCode(0);
            ajaxRespInfo.setMsg("留言成功");
        } else {
            ajaxRespInfo.setCode(-1);
            ajaxRespInfo.setMsg("系统错误");
        }
        return ajaxRespInfo;
    }


}
